package control.panel
{
	import core.View;
	
	import events.PlayEvent;
	import events.SitEvent;
	
	import flash.events.TimerEvent;
	
	import global.GameState;
	import global.GameType;
	import global.Message;
	
	import model.card.CardData;
	import model.card.ICardData;
	import model.play.PlayerModel;
	
	import utils.EnterFrameTimer;
	import utils.ai.CardTools;
	import utils.rule.IRuleResult;
	
	import view.card.Card;
	import view.card.ICard;
	import view.panel.PlayPanel;

	public class PlayPanelControl extends PanelControl
	{
		
		private var playerModel:PlayerModel;
		private var currentCards:Array;
		
		private var currentCycleHints:Array;
		private var currentCycleHintIndex:int;
		private var autoPlayTimer:EnterFrameTimer;
		private var currentState:int;
		
		
		public function PlayPanelControl(playerModel:PlayerModel)
		{
			this.playerModel = playerModel;
			initPlayPanel();
			initListeners();
			initAutoPlayTimer();
		}
		
		private function initAutoPlayTimer():void
		{
			autoPlayTimer = new EnterFrameTimer(2000);
			autoPlayTimer.addEventListener(TimerEvent.TIMER,onAutoPlayTimer);
		}
		
		private function initListeners():void
		{
			playerModel.addEventListener(PlayEvent.SHOW_PLAYER_CARDS,onShowPlayrCards);
			_model.sitModel.addEventListener(SitEvent.REMOVE,onSitRemove);	
		}
		
		private function onSitRemove(event:SitEvent):void
		{
			if (event.sitData.id == GameState.playerCurrentSitId)
			{
				GameState.playerCurrentSitId = -1;
				GameState.isSited = false;
				GameState.isAutoPlay = false;
			}
		}
		
		private function onShowPlayrCards(event:PlayEvent):void
		{
			var cardDatas:Array = event.cardDatas;
			var cardData:ICardData;
			var cards:Array = [];
			for (var i:String in cardDatas)
			{
				cardData = cardDatas[i];
				var card:ICard = new Card();
				card.updateData(cardData);
				cards.push(card);
			}
			currentCards = cards;
			_view.playPanel.clearLayourCards();
			_view.playPanel.layoutCards(cards);
		}
		
		private function initPlayPanel():void
		{
			//panel
			var playPanel:PlayPanel = new PlayPanel(this);
			playPanel.y = 440;
			View.getInstance().tableLayer.addChild(playPanel);
			View.getInstance().playPanel = playPanel;
		}
		
		public function updatePlayState(state:int):void
		{
			currentState = state;
			
			switch(state)
			{
				case GameType.SIT_STATE_NULL://等待就坐
					GameState.isRoundStarted = false;
					_view.playPanel.hideAllBut(false);
					_control.famusRemarkControl.show();
					_view.playPanel.clearLayourCards();
					autoPlayTimer.stop();
				
					break;
				case GameType.SIT_STATE_START_PLAY://开始游戏
					GameState.isRoundStarted = true;
					_view.playPanel.switchToWait();
					_view.playPanel.enableCardContainer();
					break;
				case GameType.SIT_STATE_WAITING://玩家等待其他玩家出牌
					GameState.isRoundStarted = true;
					_view.playPanel.switchToWait();
					_view.playPanel.enableCardContainer();
					break;
				case GameType.SIT_STATE_PLAYING://玩家出牌中
					GameState.isRoundStarted = true;
					_view.playPanel.enableCardContainer();
					if (GameState.isAutoPlay)
					{
						inAllHoldCards();
						_view.playPanel.switchToWait();
						_control.playPanelControl.autoPlay();
						
						return;
					}
					
					
					if (_model.tableModel.lastShowCardDatas != null)
					{
						_view.playPanel.switchToReplayState();
					}
					else
					{
						_view.playPanel.switchToPlayState();
					}
					_control.soundControl.playSound(GameType.SOUND_PLAY_NOW);
					break;
				case GameType.SIT_STATE_SITTING://游戏中
					GameState.isRoundStarted = true;
					_view.playPanel.hideAllBut(true);
					_view.playPanel.enableCardContainer();
					_view.playPanel.switchToWait();
					_control.famusRemarkControl.hide();
					break;
			}
			
			
			
		}
		
		public function onCardClick(cardId:int):void
		{
			var card:ICard;
			for (var i:String in currentCards)
			{
				card = currentCards[i];
				if (card.id == cardId)
				{
					card.playState = (card.playState == GameType.CARD_PLAY_STATE_IN)?GameType.CARD_PLAY_STATE_OUT:GameType.CARD_PLAY_STATE_IN;
					return;
				}
			}
		}
		
		
		public function updateBottomHint(content:String):void
		{
			_view.playPanel.updateBottomHint(content);
		}
		
		public function updatePlayCardsAlpha(alpha:Number):void
		{
			_view.playPanel.showCardContainerAlpha(alpha);
		}
		//提示玩家出牌
		public function hintPlayerToPlay(v:Boolean):void
		{
			_view.playPanel.yellowFrame = v;
		}
		
		public function circleHintPlayer():void
		{
			_view.playPanel.hintFrame();
		}
		
		public function onSendChat():void//发言
		{
			_control.keyboardControl.sendChatMsg(true);
		}
		
		public function onFastReply():void//快速发言
		{
			_control.fastReplayPanelControl.open();
		}
		
		//托管
		public function autoPlay():void
		{
			if (GameState.isAutoPlay && currentState == GameType.SIT_STATE_PLAYING)
			{
				autoPlayTimer.start();
			}
		}
		
		private function onAutoPlayTimer(event:TimerEvent):void
		{
			if (_model.tableModel.lastShowCardDatas != null)
			{
				onHint();
				onPlay(false);
			}
			else
			{
				var currentHoldCardDatas:Array = _model.playerModel.currentHoldCardDatas;
				var jokers:Array = CardTools.getJokers(currentHoldCardDatas);
				
				if (jokers.length == 2 && currentHoldCardDatas.length == 2)
				{
					outCards(jokers);
				}
				else
				{
					var cardData:CardData;
					for( var i:int=currentHoldCardDatas.length - 1; i>=0;i--)
					{
						cardData = currentHoldCardDatas[i];
						if (cardData.value != GameType.CARD_VALUE_JOKER)
						{
							cardData.playState = GameType.CARD_PLAY_STATE_OUT;
							break;
						}
					}	
				}
				
				onPlay(false);
			}
			autoPlayTimer.reset();
		}
		
		private function outCards(cardDatas:Array):void
		{
			for each (var cardData:CardData in cardDatas)
			{
				cardData.playState = GameType.CARD_PLAY_STATE_OUT;
			}
		}
		
		public function onAutoBut():void
		{
			_model.netRemote.sendAuto(String(int(GameState.isAutoPlay)));
		}
		
		public function onAutoServer():void
		{	
			
			updatePlayState(currentState);

			/*if (GameState.isAutoPlay)
			{
				inAllHoldCards();
				_control.playPanelControl.autoPlay();
				_view.playPanel.switchToWait();
			}
			else
			{
				updatePlayState(currentState);
				autoPlayTimer.reset();
			}*/
		}
		
		public function onHint():void//提示
		{
			var currentHoldCardDatas:Array = _model.playerModel.currentHoldCardDatas;
			if (currentCycleHints != null)
			{
				inAllHoldCards();
				var cardDatas:Array = currentCycleHints[currentCycleHintIndex];
				var cardData:ICardData;
				var holdCardData:ICardData;
				for (var i:String in cardDatas)
				{
					cardData = cardDatas[i];
					for (var n:String in currentHoldCardDatas)
					{
						holdCardData = currentHoldCardDatas[n];
						if (holdCardData.id == cardData.id)
						{
							holdCardData.playState = GameType.CARD_PLAY_STATE_OUT;
							break;
						}		
					}
				}
				currentCycleHintIndex ++;
				if (currentCycleHintIndex >= currentCycleHints.length)
				{
					currentCycleHintIndex = 0;
				}
				return;
			}
			
			
			
			var lastShowCardDatas:Array = _model.tableModel.lastShowCardDatas;
			if (lastShowCardDatas != null && lastShowCardDatas.length > 0)
			{
				var ruleResult:IRuleResult;
				ruleResult = _model.playCardModel.autoPlayCardDatas(lastShowCardDatas,currentHoldCardDatas);
				if (ruleResult.correct == true)
				{
					currentCycleHints = ruleResult.results;
					onHint();
				}
				else
				{
					onPass();
				}
			}
			
		}
		
		public function onPass():void//过牌
		{
			inAllHoldCards();
			_model.playerModel.sendPlayPass();
			clearLastShowCardDatas();
		}
		
		private function inAllHoldCards():void
		{
			var card:ICard;
			for (var i:String in currentCards)
			{
				card = currentCards[i];
				card.playState = GameType.CARD_PLAY_STATE_IN;
			}
		}
		
		public function clearLastShowCardDatas():void
		{
			//_model.tableModel.clearRoundShowCards();
			currentCycleHints = null;
			currentCycleHintIndex = 0;
		}
		
		public function onPlay(isHint:Boolean = true):void//出牌
		{
			var cardData:ICardData;
			var cardDatas:Array = [];
			var currentHoldCardDatas:Array = _model.playerModel.currentHoldCardDatas;
			var lastShowCardDatas:Array = _model.tableModel.lastShowCardDatas;

			if (currentHoldCardDatas)
			{
				for (var i:String in currentHoldCardDatas)
				{
					cardData = currentHoldCardDatas[i];
					if (cardData.playState == GameType.CARD_PLAY_STATE_OUT)
					{
						cardDatas.push(cardData);
					}
				}
				
				if (cardDatas.length == 0)
				{
					if (isHint)
					{
						updateBottomHint(Message.PICKUP_CARDS);
					}
					return;
				}
				
				var ruleResult:IRuleResult
				if (lastShowCardDatas == null)
				{
					ruleResult = _model.playCardModel.checkPlayCardDatas(cardDatas);
				}
				else
				{
					ruleResult = _model.playCardModel.playLastCardDatas(lastShowCardDatas,cardDatas);
				}
				
				if (ruleResult.correct == true)
				{
					if (ruleResult.results && ruleResult.results.length > 1)
					{
						_view.promptSelectCombineCardsPanel.updateCombineCards(ruleResult.results);
						_view.promptSelectCombineCardsPanel.open();
						return;
					}
					
					_model.playerModel.sendPlayCards(ruleResult.results[0]);
				}
				else
				{
					updateBottomHint(Message.RULE_BREAK);
				}
				
			}
			clearLastShowCardDatas();
		}
		
		public function onEmote():void
		{
			_control.emotePanelControl.open();
		}
		
		public function clear():void
		{
			updatePlayState(GameType.SIT_STATE_NULL);
			this.clearLastShowCardDatas();
			currentCards = null;
			_view.playPanel.clear();
			currentCycleHints = null;
			currentCycleHintIndex = 0;
		}
	}
}